<script>
import {
  getBookstoreJsonData,
  getBookstoreData,
} from "/assets/api/bookstore.js";
import { onBeforeMount, onBeforeUnmount, onMounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Toast } from "vant";
export default {
  setup() {
    const router = useRouter();
    let userinfo = JSON.parse(localStorage.getItem("userinfo"));
    let jump = () => {
      if (userinfo.usermsg.type == "no") {
        Toast("游客登录不可签到");
        return;
      }
      router.push("/sign");
    };

    let listObj = reactive({
      freeList: [],
      vipList: [],
    });
    // 数据初始化
    let dateInit = async () => {
      let data = await getBookstoreData().catch(() =>
        console.log("接口调用失败")
      );
      if (!data) data = await getBookstoreJsonData();
      // console.log(data);
      listObj.freeList = JSON.parse(JSON.stringify(data.freeList));
      listObj.vipList = JSON.parse(JSON.stringify(data.vipList));
    };

    // 定义响应式数据 服务于滚动条
    let BookStoreRef = ref(null);
    let Alpha = reactive({
      background: 0,
      color: 1,
      show: false,
    });

    // 滚轮事件
    let __scrollInit = () => {
      // let target = BookCityRef.value.parentElement;
      let target = document.querySelector("#app");
      // console.dir("滚动视图原生对象", target);

      target.onscroll = (e) => {
        // console.dir(e.target.scrollTop);
        let scrollTop = e.target.scrollTop;
        let top = 185;
        Alpha.background = scrollTop / top;
        Alpha.color = 1 - scrollTop / top;
        Alpha.show = scrollTop / top > 1 ? true : false;
        // console.log(Alpha);
      };
    };

    let __scrollDestroy = () => {
      // let target = BookCityRef.value.parentElement;
      let target = document.querySelector("#app");
      target.onscroll = null;
      console.log("成功清除滚动条事件");
    };

    // 挂载后
    onMounted(() => {
      // 滚轮事件
      __scrollInit();
    });

    // 挂载前
    onBeforeMount(() => {
      // 数据初始化
      dateInit();
    });

    onBeforeUnmount(() => {
      // 滚轮事件清除
      __scrollDestroy();
    });

    let goVIP = () => {
      if (userinfo.usermsg.type == "no") {
        Toast("游客登录不可买会员");
        return;
      }
      router.push("/Member");
    };

    // 跳到详情页
    let details = (id) => {
      router.push({ path: "/Details", query: { id: id } });
    };

    let end = () => {
      router.push("/End");
    };

    return {
      end,
      toast: Toast,
      details,
      BookStoreRef,
      listObj,
      Alpha,
      jump,
      goVIP,
      userinfo,
    };
  },
};
</script>

<template>
  <div class="BookStore" ref="BookStoreRef">
    <div class="store_top">
      <p
        :class="['title', { con: Alpha.show }]"
        :style="{
          color: `rgba(255, 255, 255, ${Alpha.color})`,
          background: `rgba(255, 255, 255, ${Alpha.background})`,
        }"
      >
        有狐惠
      </p>
      <div class="bg">
        <img src="/assets/img/store.bg.png" alt="" />
      </div>
      <div class="vip_box" @click="goVIP">
        <div class="boxs">
          <h2 class="titles">有狐尊享会员</h2>
          <p class="name">畅享海量小说，点击查看会员尊享特权</p>
          <span class="go_vip">加入会员</span>
          <div class="imgs">
            <img src="/assets/img/sto.png" alt="" />
          </div>
        </div>
      </div>
    </div>
    <div class="store_tab">
      <ul>
        <li @click="jump">
          <div class="img">
            <img src="/assets/img/store_01.png" alt="" />
          </div>
          <p class="tit">签到任务</p>
        </li>
        <li>
          <div class="img" @click="toast('特价优惠')">
            <img src="/assets/img/store_02.png" alt="" />
          </div>
          <p class="tit">特价优惠</p>
        </li>
        <li>
          <div class="img" @click="toast('包月礼包')">
            <img src="/assets/img/store_03.png" alt="" />
          </div>
          <p class="tit">包月礼包</p>
        </li>
      </ul>
    </div>
    <div class="welfare">
      <div class="welfare_top">
        <p class="we">福利活动</p>
        <p class="move" @click="end">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="wefare_box">
        <div class="wefare_item" @click="toast('一元购')">
          <div class="item_bg">
            <img src="/assets/img/tab01.png" alt="" />
          </div>
          <div class="text">
            <p class="big">一元购</p>
            <p class="small">白本好书一元购</p>
          </div>
        </div>
        <div class="wefare_item" @click="toast('作者福利')">
          <div class="item_bg">
            <img src="/assets/img/tab02.png" alt="" />
          </div>
          <div class="text">
            <p class="big">作者福利</p>
            <p class="small">会员好礼送不停</p>
          </div>
        </div>
        <div class="wefare_item" @click="toast('限时折扣')">
          <div class="item_bg">
            <img src="/assets/img/tab03.png" alt="" />
          </div>
          <div class="text">
            <p class="big">限时折扣</p>
            <p class="small">读万卷书行万里路</p>
          </div>
        </div>
        <div class="wefare_item" @click="toast('新书5元尝鲜')">
          <div class="item_bg">
            <img src="/assets/img/tab04.png" alt="" />
          </div>
          <div class="text">
            <p class="big">新书5元尝鲜</p>
            <p class="small">最新最全的在这里</p>
          </div>
        </div>
      </div>
    </div>

    <div class="free" v-if="listObj.freeList.length">
      <div class="welfare_top">
        <p class="we">限时免费</p>
        <p class="move" @click="end">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="i in 6"
          :key="listObj.freeList[i - 1].BookId"
          @click="details(listObj.freeList[i - 1].BookId)"
        >
          <div class="img">
            <img :src="listObj.freeList[i - 1].BookImage" alt="" />
            <span class="over" v-if="listObj.freeList[i - 1].isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">
              {{ listObj.freeList[i - 1].BookName }}
            </p>
            <p class="names">{{ listObj.freeList[i - 1].Author }}</p>
          </div>
        </div>
      </div>
    </div>
    <div class="free" v-if="listObj.vipList.length">
      <div class="welfare_top">
        <p class="we">会员书籍精选</p>
        <p class="move" @click="end">
          更多<span class="iconfont icon-xiangyou1"></span>
        </p>
        <span class="i"></span>
      </div>
      <div class="free_box">
        <div
          class="free_item"
          v-for="i in 6"
          :key="listObj.vipList[i - 1].BookId"
          @click="details(listObj.vipList[i - 1].BookId)"
        >
          <div class="img">
            <img :src="listObj.vipList[i - 1].BookImage" alt="" />
            <span class="over" v-if="listObj.vipList[i - 1].isEnd">完结</span>
          </div>
          <div class="p">
            <p class="name text-ellipsis-1">
              {{ listObj.vipList[i - 1].BookName }}
            </p>
            <p class="names">{{ listObj.vipList[i - 1].Author }}</p>
          </div>
        </div>
      </div>
    </div>

    <BottomNavigationBar current="3" />
  </div>
</template>



<style lang="scss" scoped>
.BookStore {
  min-height: 100%;
  min-width: 375px;
  max-width: 750px;
  width: 100%;
  box-sizing: border-box;
  padding-bottom: 65px;
  .store_top {
    width: 100%;
    position: relative;
    .title {
      position: fixed;
      top: -1px;
      left: 0;
      right: 0;
      height: 50px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      box-sizing: border-box;
      font-size: 22px;
      color: rgba(255, 255, 255, 1);
      background-color: rgba(255, 255, 255, 0);
      opacity: 0.98;
      z-index: 10;
      &.con {
        justify-content: center;
        font-size: 18px;
        font-weight: bold;
        box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.2);
        color: #333 !important;
      }
    }
    .bg {
      width: 100%;
      border-bottom-left-radius: 50% 45px;
      border-bottom-right-radius: 50% 45px;
      overflow: hidden;
      img {
        width: 101%;
      }
    }
    .vip_box {
      padding: 0 20px;
      height: 164px;
      position: absolute;
      top: 82px;
      left: 0;
      width: 100%;
      box-sizing: border-box;
      .boxs {
        width: 100%;
        height: 164px;
        border-radius: 8px;
        background-image: linear-gradient(to right, #f49b46, #ec265d);
        position: relative;
        .titles {
          position: absolute;
          top: 25px;
          left: 25px;
          font-size: 20px;
          color: #fff;
        }
        .name {
          font-size: 12px;
          color: #ffc9cf;
          position: absolute;
          top: 60px;
          left: 25px;
        }
        .imgs {
          position: absolute;
          right: 3px;
          bottom: 2px;
          width: 120px;
          height: 120px;
        }
        .go_vip {
          position: absolute;
          left: 25px;
          bottom: 28px;
          height: 32px;
          line-height: 32px;
          padding: 0 22px;
          border-radius: 22px;
          background-color: #fff;
          color: #fd697f;
          font-size: 14px;
        }
      }
    }
  }
  .store_tab {
    padding: 0 20px;
    margin-top: 90px;
    ul {
      padding: 0 32px;
      display: flex;
      justify-content: space-between;
      li {
        display: flex;
        flex-direction: column;
        .img {
          width: 54px;
          border-radius: 50%;
          overflow: hidden;
        }
        .tit {
          font-size: 13px;
          color: #56556a;
          margin-top: 12px;
        }
      }
    }
  }
  .welfare {
    margin-top: 20px;
    padding: 0 20px;
    position: relative;

    .wefare_box {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      .wefare_item {
        width: 48%;
        height: 95px;
        margin-top: 19px;
        border-radius: 6px;
        overflow: hidden;
        position: relative;
        .item_bg {
          width: 100%;
          height: 100%;
        }
        .text {
          position: absolute;
          top: 19px;
          left: 14px;
          .big {
            font-size: 14px;
            color: #fff;
            font-weight: bold;
          }
          .small {
            font-size: 12px;
            color: #fff;
            opacity: 0.6;
          }
        }
      }
    }
  }
  .welfare_top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .we {
      font-size: 18px;
      color: #28282e;
      font-weight: bold;
    }
    .move {
      font-size: 12px;
      color: #9ea0ad;
      .icon-xiangyou1 {
        font-size: 10px;
      }
    }
    .i {
      position: absolute;
      left: 0;
      top: 5px;
      width: 4px;
      height: 18px;
      background-color: #f1325e;
    }
  }
  .free {
    padding: 0 20px;
    margin-top: 20px;
    position: relative;
    .free_box {
      display: flex;
      flex-wrap: wrap;
      .free_item {
        margin-top: 19px;
        width: 100px;
        // margin-right: 17px;
        margin-right: calc((100% - 300px) / 2);
        &:nth-child(3n) {
          margin-right: 0;
        }
        .img {
          width: 100%;
          position: relative;
          height: 130px;
          .over {
            position: absolute;
            bottom: 8px;
            right: 7px;
            color: #fff;
            opacity: 0.7;
            background-color: rgba(0, 0, 0, 0.5);
            font-size: 11px;
            height: 20px;
            line-height: 20px;
            padding: 0 5px;
            border-radius: 2px;
          }
        }
        .p {
          display: flex;
          flex-direction: column;
          margin-top: 6px;
          .name {
            font-size: 12px;
            color: #686878;
          }
          .names {
            transform: scale(0.6);
            color: #aaaaad;
            margin-left: -24px;
          }
        }
      }
    }
  }
}
</style>